<template>
  <div class="app-container">
    <!-- 角色抽屉 -->
    <el-drawer
      title="分配角色权限"
      :visible.sync="visible"
      :direction="direction"
      size="25%"
    >
      <div class="app-container">
        <el-tree
          ref="tree"
          :data="selectPermissions"
          show-checkbox
          node-key="id"
          :default-expand-all="true"
        />
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click.native.prevent="updateRolePermission"
        >
          更新权限
        </el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import permission from '@/api/system/permission'
import rolePermission from '@/api/system/rolePermission'
export default {
  name: 'RoleDrawer',
  filters: {},
  props: {
    permissions: {
      type: Array
    }
  },
  data() {
    return {
      selectPermissions: [],
      loading: true,
      selectedIds: [],
      selectedRoles: [],
      hadRole: [],
      visible: false,
      direction: 'rtl',
      roleId: '',
      hasPermission: []
    }
  },
  watch: {
    permissions() {
      // 封装树形数据格式
      function loadPermission(permission) {
        // console.log(permission);
        const node = {
          id: permission.id,
          label: permission.name,
          children: []
        }
        // console.log(node);
        // 如果有子节点
        if (permission.children && permission.children.length > 0) {
          permission.children.forEach((child) => {
            // console.log(loadPermission(child));
            node.children.push(loadPermission(child))
          })
        }
        return node
      }
      this.permissions.forEach((permission) => {
        this.selectPermissions.push(loadPermission(permission))
      })
    }
  },
  created() {
    // 备份默认值
  },
  methods: {
    updateRolePermission() {
      rolePermission
        .updateRolePermission({
          roleId: this.roleId,
          permissionIds: this.$refs.tree.getCheckedKeys().join(',')
        })
        .then(
          (res) => {
            this.$message({
              type: 'success',
              message: res.message
            })
            this.loading = false
            this.visible = false
          },
          (err) => {
            this.$message({
              type: 'error',
              message: err.message
            })
            this.loading = false
            this.visible = false
          }
        )
    },
    // 获得树key
    getCheckedKeys() {
      console.log(this.$refs.tree.getCheckedKeys())
    },
    // 通过key设置
    setCheckedKeys(permissionIds) {
      this.$refs.tree.setCheckedKeys(permissionIds)
    },
    // 通过节点设置
    setCheckedNodes(hasPermission) {
      this.$refs.tree.setCheckedNodes(hasPermission)
    },
    // 获得角色权限
    getRolePermission() {
      this.hasPermission = []
      permission.getRolePermission({ roleId: this.roleId }).then((res) => {
        function getHasPermission(permission, hasPermission) {
          if (permission.children && permission.children.length > 0) {
            permission.children.forEach((child) => {
              getHasPermission(child, hasPermission)
            })
          } else {
            hasPermission.push({ id: permission.id, label: permission.name })
          }
        }
        if (res.result && res.result.length > 0) {
          res.result.forEach((permission) => {
            getHasPermission(permission, this.hasPermission)
          })
        }
        this.setCheckedNodes(this.hasPermission)
      })
    },
    defaultMethod() {},
    // 获取分页数据
    getTableData(arg) {},
    // 分页改变
    handleCurrentChange(currentPage) {
      this.queryParam.pageNo = currentPage
      this.getTableData()
    },
    // 添加
    handleAdd() {
      console.log('handleAdd')
    },
    // 删除
    handleDelete(id) {
      console.log('handleDelete')
    },
    // 编辑
    handleEdit(index, row) {
      console.log('handleEdit')
    },
    // 搜索
    searchQuery() {
      this.getTableData(1)
    },
    // 重置
    reset() {
      this.getTableData()
    },
    // 增删改刷新数据
    handleOK() {
      this.selectedIds = []
      this.selectedDataAll = []
      this.getTableData()
    }
  }
}
</script>
